package com.study.juc.atomics;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicMarkableReference;

/**
 * @program: juc-study
 * @author: jzhou
 * @date: 2022-08-16 22:23
 * @version: 1.0
 * @description: markable 原子标记为演示 ,只记录是否被更改，无法获取更改了几个版本
 **/
public class AtomicMarkableReferenceDemo {
    static AtomicMarkableReference<Integer> markableReference = new AtomicMarkableReference<>(100,false);
    public static void main(String[] args) {
        new Thread(()->{
            boolean marked = markableReference.isMarked();
            System.out.println(Thread.currentThread().getName() + "\t 默认标识为：" + marked);
            try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            markableReference.compareAndSet(100,1000,marked,!marked);
        },"t1").start();


        new Thread(()->{
            boolean marked = markableReference.isMarked();
            System.out.println(Thread.currentThread().getName() + "\t 默认标识为：" + marked);
            try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
            boolean b = markableReference.compareAndSet(100, 2000, marked, !marked);
            System.out.println(Thread.currentThread().getName() +"\t 是否改变结果"+ b);
            System.out.println(Thread.currentThread().getName() +"\t"+ markableReference.isMarked());
            System.out.println(Thread.currentThread().getName() +"\t"+ markableReference.getReference());
        },"t2").start();
    }
}
